package com.jbit.dm212.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jbit.dm212.common.RedisUtils;
import com.jbit.dm212.common.ResponseResult;
import com.jbit.dm212.common.ResponseResultUtils;
import com.jbit.dm212.common.UserUtil;
import com.jbit.dm212.entity.dto.DmUserDTO;
import com.jbit.dm212.entity.po.DmLinkUser;
import com.jbit.dm212.entity.po.DmUser;
import com.jbit.dm212.entity.vo.DmLinkUserVO;
import com.jbit.dm212.entity.vo.DmUserVO;
import com.jbit.dm212.service.DmLinkUserService;
import com.jbit.dm212.service.DmUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user/api/v")
public class DmUserVerifyController {
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private DmLinkUserService linkUserService;

    @Resource
    private DmUserService userService;
    @PostMapping("/loadCurrentUserByToken")
    public ResponseResult loadCurrentUserByToken(@RequestBody DmUserVO userVO){
        if (redisUtils.hasKey(userVO.getToken())){
            DmUser user = redisUtils.get(userVO.getToken(),DmUser.class);
            return ResponseResultUtils.sendSuccess(user);
        }else{
            return ResponseResultUtils.sendError("0001");
        }
    }

    @PostMapping("/queryLinkUser")
    public ResponseResult<List<DmLinkUser>> queryLinkUser(){
        DmUser user = UserUtil.getUser();
        List<DmLinkUser> linkUsers = linkUserService.getLinkUserByUserId(user.getId());
        return ResponseResultUtils.sendSuccess(linkUsers);
    }

    @PostMapping("/addLinkUser")
    public ResponseResult addLinkUser(@RequestBody DmLinkUserVO linkUserVO){
        DmUser user = UserUtil.getUser();
        DmLinkUser linkUser = BeanUtil.toBean(linkUserVO,DmLinkUser.class);
        linkUser.setUserId(user.getId());
        linkUserService.addLinkUser(linkUser);
        return ResponseResultUtils.sendSuccess();
    }

    @PostMapping("/queryLinkUserByIdCard")
    public ResponseResult queryLinkUserByIdCard(@RequestBody DmLinkUserVO linkUserVO){
        DmUser user = UserUtil.getUser();
        DmLinkUser linkUser = BeanUtil.toBean(linkUserVO,DmLinkUser.class);
        linkUser.setUserId(user.getId());
        if(linkUserService.checkIdCard(linkUser)){
            return ResponseResultUtils.sendError("1009");
        }else{
            return ResponseResultUtils.sendSuccess();
        }
    }

    @PostMapping("/queryUserInfoByToken")
    public ResponseResult queryUserInfoByToken(@RequestHeader String token){
        DmUser user = UserUtil.getUser();
        DmUserDTO userDTO = userService.login(user);
        redisUtils.set(token,userDTO.getUser());
        return ResponseResultUtils.sendSuccess(userDTO.getUser());
    }

    @PostMapping("/modifyUserInfo")
    public ResponseResult modifyUserInfo(@RequestBody DmUser user){
        user.setUpdatedTime(new Date());
        userService.updateUser(user);
        return ResponseResultUtils.sendSuccess();
    }

    @PostMapping("/deleteLinkUserById")
    public ResponseResult deleteLinkUserById(@RequestBody DmLinkUser linkUser){
        boolean flag = linkUserService.deleteLinkUserById(linkUser.getId());
        return ResponseResultUtils.sendSuccess();
    }
}
